package com.xm;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Author：xm
 * Project：interview
 * Date：2025/8/5  16:39
 */

public class _01_SynchronizedMethod {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        executorService.execute(() -> _01_SynchronizedMethod.staticMethod());
        executorService.execute(() -> _01_SynchronizedMethod.staticMethod());

        executorService.execute(() -> {
            _01_SynchronizedMethod method = new _01_SynchronizedMethod();
            method.method();
        });
        executorService.execute(() -> {
            _01_SynchronizedMethod method2 = new _01_SynchronizedMethod();
            method2.method();
        });

        // _01_SynchronizedMethod method = new _01_SynchronizedMethod();
        // executorService.execute(() -> {
        //     method.method();
        // });
        // executorService.execute(() -> {
        //     method.method();
        // });

        executorService.shutdown();
    }

    // synchronized用在方法上，称为同步方法
    public synchronized void method() {
        System.out.println("普通方法执行时间：" + LocalDateTime.now());
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void staticMethod() {
        System.out.println("静态方法执行时间：" + LocalDateTime.now());
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
